package com.example.spring.demo.controller;

import com.example.spring.demo.service.MinioService;
import io.minio.ObjectWriteResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;

//控制
@RestController
@RequestMapping("/api/upload")
@RequiredArgsConstructor
public class ImageUploadController {
    private final MinioService minioService;

    @RequestMapping("/uploadImage")
    public String uploadImage(@RequestParam("file") MultipartFile file) throws Exception {  //112123.jpg
        String Url = "";
        //获取文件名
        String fileName = file.getOriginalFilename();// 112123
        //获取文件扩展名
        String fileExtenionName = fileName.substring(fileName.indexOf(".")); //.jpg
        //新的文件名
        String newName = UUID.randomUUID() +fileExtenionName; //81238618268.jpg
        // 文件保存到服务器
        minioService.uploadFile(newName, file);
        // 获取文件的URL
        return minioService.getObjectUrl(newName);
    }
}
